﻿Imports Spring.Transaction.Interceptor
Imports Spring.Transaction

<Serializable()> _
Public Class Loja
    Inherits BaseDominio

    Private _nome As String
    Public Overridable Property Nome() As String
        Get
            Return _nome
        End Get
        Set(ByVal value As String)
            _nome = value
        End Set
    End Property

    Private _CNPJ As String
    Public Overridable Property CNPJ() As String
        Get
            Return _CNPJ
        End Get
        Set(ByVal value As String)
            _CNPJ = value
        End Set
    End Property

    Private _enderecos As IList(Of Endereco)
    Public Overridable Property Enderecos() As IList(Of Endereco)
        Get
            Return _enderecos
        End Get
        Set(ByVal value As IList(Of Endereco))
            _enderecos = value
        End Set
    End Property


#Region "Negocio"

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overridable Function Buscar(ByVal _id As Long) As Loja
        Return MyBase.LojaDao.Buscar(_id)
    End Function

    <Transaction(TransactionPropagation.Required)> _
    Public Overridable Sub Salvar()

        If Me.PodeSalvar Then
            MyBase.LojaDao.Salvar(Me)
        End If
    End Sub

    Public Overridable Function PodeSalvar() As Boolean

        If Me.Nome = String.Empty Then Throw New ArtNoiException("É preciso informar o nome")
        If Me.CNPJ = String.Empty Then Throw New ArtNoiException("É preciso informar o CNPJ")
        If Me.Enderecos Is Nothing Then Throw New ArtNoiException("É preciso informar ao menos um endereço")
        For Each _endereco In Me.Enderecos
            If _endereco Is Nothing Then Throw New ArtNoiException("O endereço informado está incorreto.")
            If Not _endereco.Id.HasValue Then Throw New ArtNoiException("O endereço informado está não foi salvo")
        Next

        Return True

    End Function

#End Region

End Class

